package com.jingran.config;

import com.jingran.cache.RedisCache;
import com.jingran.entity.UserEntity;
import com.jingran.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截器和过滤器配置
 * @author jingran
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) response;
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setHeader("Access-Control-Allow-Methods", "POST");
        //res.setHeader("Access-Control-Allow-Headers", "Access-Control");
        res.setHeader("Access-Control-Allow-Headers", "*");
        res.setHeader("Allow", "POST");
        chain.doFilter(request, response);

    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
                //因为会发送两次请求，第一次过了才会走第二次
                if(request.getMethod().equals("OPTIONS")){
                    response.setStatus(200);
                    return true;
                }
                String token = request.getHeader("token");
                if(!StringUtils.hasText(token)){
                    response.setStatus(401);
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write("您没有权限");
                    return false;
                }else {
                    RedisCache redisCache = SpringContextUtils.getBean(RedisCache.class);
                    UserEntity sessionData = redisCache.getSessionData(token);
                    if(sessionData != null){
                        return true;
                    }else {
                        response.setStatus(401);
                        response.setContentType("text/html;charset=utf-8");
                        response.getWriter().write("token不正确");
                        return false;
                    }
                }
            }
        })
                .addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/kaptcha")
                .excludePathPatterns("/jingran/**");
    }
}
